#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 1e6 + 5;
int n, a[N], lim;
int e_cnt = 1, heads[N];
bool vis[N], vis2[N];
ll ans, f[2][N];

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

void solve(int);

bool dfs(int x) {
  vis[x] = true;
  for (int i = heads[x], v; i; i = e[i].nxt) {
    if ((i & 1) == 0) {
      if (vis[v = e[i].v]) {
        lim = x;
        solve(x);
        ll tmp = f[0][x];
        lim = v;
        solve(v);
        tmp = std::max(tmp, f[0][v]);
        ans += tmp;
        return true;
      }
      if (dfs(v)) return true;
    }
  }
  return false;
}

void solve(int x) {
  vis[x] = true;
  f[0][x] = 0, f[1][x] = a[x];
  for (int i = heads[x], v; i; i = e[i].nxt) {
    if (i & 1 && (v = e[i].v) != lim) {
      solve(v);
      f[0][x] += std::max(f[0][v], f[1][v]);
      f[1][x] += f[0][v];
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("P2607 [ZJOI2008]骑士.in", "r", stdin);
  freopen("P2607 [ZJOI2008]骑士.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 1, fa; i <= n; ++i) {
    scanf("%d%d", &a[i], &fa);
    add(i, fa), add(fa, i);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) dfs(i);
  }
  printf("%lld", ans);
  return 0;
}